﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using YiSha.Util;

namespace YiSha.Entity.Test
{
    [Table("PMC_Process")]
    public class ProcessEntity : BaseEntity
    {
        /// <summary>
        /// 工序名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 用户ID（long类型在前端JS中会丢失精度，所以转成字符串）
        /// </summary>
        [JsonConverter(typeof(StringJsonConverter))]
        public long? UserId { get; set; }

        /// <summary>
        /// 父工序ID（long类型在前端JS中会丢失精度，所以转成字符串）
        /// </summary>
        [JsonConverter(typeof(StringJsonConverter))]
        public long? ParentId { get; set; }

        /// <summary>
        /// 部门备注
        /// </summary>
        public string DepartmentRemark { get; set; }

        /// <summary>
        /// 子工序集合（用于树形结构展示）
        /// </summary>
        [NotMapped] // 表示此字段不映射到数据库
        public List<ProcessEntity> Children { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ProcessEntity()
        {
            // 初始化子工序集合，避免null引用
            Children = new List<ProcessEntity>();
        }

        /// <summary>
        /// 是否为根节点（ParentId为null或0）
        /// </summary>
        [NotMapped]
        public bool IsRoot => !ParentId.HasValue || ParentId == 0;

        /// <summary>
        /// 是否为叶子节点（没有子工序）
        /// </summary>
        [NotMapped]
        public bool IsLeaf => Children == null || Children.Count == 0;

        /// <summary>
        /// 重写ToString方法，提供对象的字符串表示
        /// </summary>
        /// <returns>包含对象关键信息的字符串</returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendLine($"ProcessEntity [");
            sb.AppendLine($"  Id: {Id}");
            sb.AppendLine($"  Name: {Name}");
            sb.AppendLine($"  UserId: {UserId}");
            sb.AppendLine($"  ParentId: {ParentId}");
            sb.AppendLine($"  DepartmentRemark: {DepartmentRemark}");
            sb.AppendLine($"  IsRoot: {IsRoot}");
            sb.AppendLine($"  IsLeaf: {IsLeaf}");
            sb.AppendLine($"  ChildrenCount: {Children?.Count ?? 0}");
            sb.AppendLine($"]");

            return sb.ToString();
        }
    }
}
